/* 
 *  <copyright file="FPSGameService.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */


using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using SobrietyEngine.Events;

namespace SobrietyEngine.Common
{
    public class FPSGameService : DrawableGameComponent
    {
        private SpriteBatch spriteBatch;
        private SpriteFont videoFont;
        private float elapsedTime;
        private float totalFrames;
        private float fps;
        private string fontName;
        private Color textColor = Color.White;

        IEventRouter EventRouter;
        int numMessages = 0;

        public string FontName
        {
            get { return fontName; }
            set
            {
                if (string.IsNullOrEmpty(value))
                    return;

                fontName = value;
                if (spriteBatch != null)
                    videoFont = Game.Content.Load<SpriteFont>(fontName);
            }
        }

        public Color Color
        {
            get { return textColor; }
            set { textColor = value; }
        }


        public FPSGameService(Game game)
            : base(game)
        {
        }

        public FPSGameService(Game game, bool show)
            : base(game)
        {
            Visible = show;
        }

        public FPSGameService(Game game, string fontName)
            : base(game)
        {
            this.fontName = fontName;
        }

        public FPSGameService(Game game, string fontName, bool show)
            : base(game)
        {
            this.fontName = fontName;
            Visible = show;
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);

            // Load the font if fontName has been set.
            if (!string.IsNullOrEmpty(fontName))
                videoFont = Game.Content.Load<SpriteFont>(fontName);
        }

        public override void Initialize()
        {
            base.Initialize();
            EventRouter = (IEventRouter)Game.Services.GetService(typeof(IEventRouter));
        }
        int colls = 0;
        public override void Update(GameTime gameTime)
        {
            elapsedTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
            totalFrames++;

            if (elapsedTime >= 1.0f)
            {
                fps = totalFrames;
                totalFrames = 0;
                elapsedTime = 0;
            }
           // colls = ((Physics.PhysicsGameService)Game.Services.GetService(typeof(Physics.IPhysicsGameService))).NumCollisions;
           // ((Physics.PhysicsGameService)Game.Services.GetService(typeof(Physics.IPhysicsGameService))).NumCollisions = 0;
            numMessages = EventRouter.EventCount;
        }

        public override void Draw(GameTime gameTime)
        {
            
            if (fontName.Length > 0)
            {
                spriteBatch.Begin();
                spriteBatch.DrawString(
                    videoFont,
                    "EventCount=" + numMessages + "   Collisions=" + colls,
                    new Vector2(videoFont.LineSpacing),
                    textColor,
                    0f,
                    Vector2.Zero,
                    1f,
                    SpriteEffects.None,
                    0);
                spriteBatch.End();
            }
        }
    }
}